#include "udp_thread.h"
#include "windows.h"
#include "ECanVci.h"
#include <string.h>
#include "adaptor_parm.h"
#include "operate_parm.h"
#include <Windows.h>
#include <winsock2.h>
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "main.h"

using namespace std;

WSADATA my_ws;
SOCKET sock;
struct sockaddr_in server;
struct sockaddr_in local_addr;
struct sockaddr_in rx_server;
char tx_buf[512];
HANDLE mutex_udp_tx = NULL;
unsigned short udp_cmd_count = 0;

unsigned char udp_rx_buf[1536];

const unsigned int table[] = {
    0u, 79764919u, 159529838u, 222504665u, 319059676u, 398814059u, 445009330u, 507990021u, 638119352u, 583659535u,
    797628118u, 726387553u, 890018660u, 835552979u, 1015980042u, 944750013u, 1276238704u, 1221641927u, 1167319070u, 1095957929u,
    1595256236u, 1540665371u, 1452775106u, 1381403509u, 1780037320u, 1859660671u, 1671105958u, 1733955601u, 2031960084u, 2111593891u,
    1889500026u, 1952343757u, 2552477408u, 2632100695u, 2443283854u, 2506133561u, 2334638140u, 2414271883u, 2191915858u, 2254759653u,
    3190512472u, 3135915759u, 3081330742u, 3009969537u, 2905550212u, 2850959411u, 2762807018u, 2691435357u, 3560074640u, 3505614887u,
    3719321342u, 3648080713u, 3342211916u, 3287746299u, 3467911202u, 3396681109u, 4063920168u, 4143685023u, 4223187782u, 4286162673u,
    3779000052u, 3858754371u, 3904687514u, 3967668269u, 881225847u, 809987520u, 1023691545u, 969234094u, 662832811u, 591600412u,
    771767749u, 717299826u, 311336399u, 374308984u, 453813921u, 533576470u, 25881363u, 88864420u, 134795389u, 214552010u,
    2023205639u, 2086057648u, 1897238633u, 1976864222u, 1804852699u, 1867694188u, 1645340341u, 1724971778u, 1587496639u, 1516133128u,
    1461550545u, 1406951526u, 1302016099u, 1230646740u, 1142491917u, 1087903418u, 2896545431u, 2825181984u, 2770861561u, 2716262478u,
    3215044683u, 3143675388u, 3055782693u, 3001194130u, 2326604591u, 2389456536u, 2200899649u, 2280525302u, 2578013683u, 2640855108u,
    2418763421u, 2498394922u, 3769900519u, 3832873040u, 3912640137u, 3992402750u, 4088425275u, 4151408268u, 4197601365u, 4277358050u,
    3334271071u, 3263032808u, 3476998961u, 3422541446u, 3585640067u, 3514407732u, 3694837229u, 3640369242u, 1762451694u, 1842216281u,
    1619975040u, 1682949687u, 2047383090u, 2127137669u, 1938468188u, 2001449195u, 1325665622u, 1271206113u, 1183200824u, 1111960463u,
    1543535498u, 1489069629u, 1434599652u, 1363369299u, 622672798u, 568075817u, 748617968u, 677256519u, 907627842u, 853037301u,
    1067152940u, 995781531u, 51762726u, 131386257u, 177728840u, 240578815u, 269590778u, 349224269u, 429104020u, 491947555u,
    4046411278u, 4126034873u, 4172115296u, 4234965207u, 3794477266u, 3874110821u, 3953728444u, 4016571915u, 3609705398u, 3555108353u,
    3735388376u, 3664026991u, 3290680682u, 3236090077u, 3449943556u, 3378572211u, 3174993278u, 3120533705u, 3032266256u, 2961025959u,
    2923101090u, 2868635157u, 2813903052u, 2742672763u, 2604032198u, 2683796849u, 2461293480u, 2524268063u, 2284983834u, 2364738477u,
    2175806836u, 2238787779u, 1569362073u, 1498123566u, 1409854455u, 1355396672u, 1317987909u, 1246755826u, 1192025387u, 1137557660u,
    2072149281u, 2135122070u, 1912620623u, 1992383480u, 1753615357u, 1816598090u, 1627664531u, 1707420964u, 295390185u, 358241886u,
    404320391u, 483945776u, 43990325u, 106832002u, 186451547u, 266083308u, 932423249u, 861060070u, 1041341759u, 986742920u,
    613929101u, 542559546u, 756411363u, 701822548u, 3316196985u, 3244833742u, 3425377559u, 3370778784u, 3601682597u, 3530312978u,
    3744426955u, 3689838204u, 3819031489u, 3881883254u, 3928223919u, 4007849240u, 4037393693u, 4100235434u, 4180117107u, 4259748804u,
    2310601993u, 2373574846u, 2151335527u, 2231098320u, 2596047829u, 2659030626u, 2470359227u, 2550115596u, 2947551409u, 2876312838u,
    2788305887u, 2733848168u, 3165939309u, 3094707162u, 3040238851u, 2985771188u};

unsigned int getCRC(unsigned char *buf, int nLength)
{
    unsigned int num = 0xffffffff;
    for (unsigned int num2 = 0; num2 < nLength; num2++)
    {
        num = (num << 8) ^ table[(num >> 24) ^ buf[num2]];
    }

    return num;
}

int verify_udp_frame(unsigned char *data, unsigned int len)
{
    int ret = 0;
    unsigned short frame_len = *((unsigned short *)&data[8]);
    unsigned short frame_len_inverse = *((unsigned short *)&data[10]);
    unsigned int check_sum = 0;
    char *protocol_name = "CX100000";

    if ((*((unsigned int *)&protocol_name[0]) == *((unsigned int *)&data[0])) && (*((unsigned int *)&protocol_name[4]) == *((unsigned int *)&data[4])) && (frame_len == (unsigned short)(~frame_len_inverse)) && (frame_len == len) && (1500 > len))
    {
        for (unsigned int i = 0; i < len - 4; i++)
            check_sum += data[i];

        if (check_sum == *((unsigned int *)&data[len - 4]))
            ret = 0;
        else
            ret = -1;
    }
    else
        ret = -1;

    return ret;
}

string udp_str_tmp;
char udp_str_buf[64];
struct timeb t1;
long long time_last;
unsigned char udp_stop = 0;
DWORD WINAPI udp_process(LPVOID pParam)
{
    int ret = 0;
    int nrecv = 0;
    unsigned long ipthread = 0;
    unsigned short cmd_code = 0;
    unsigned char ack = 0;
    unsigned char is_ack = 0;
    int frame_len = 0;
    unsigned int result = 0;

    time_last = time(NULL);
    ftime(&t1);
    udp_stop = 0;
    if (WSAStartup(MAKEWORD(2, 2), &my_ws) != 0)
    {
        ret = -1;
    }

    sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock == INVALID_SOCKET)
    {
        ret = -2;
    }

    char hostname[24];
    gethostname(hostname, sizeof(hostname));

    server.sin_family = AF_INET;
    server.sin_addr.S_un.S_addr = inet_addr(lineedit_summary_13.lineedit_3->lineedit->text().toStdString().c_str());
    server.sin_port = htons(lineedit_summary_13.lineedit_4->lineedit->text().toInt());
    int len = sizeof(server);

    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.S_un.S_addr = inet_addr(lineedit_summary_13.lineedit_1->lineedit->text().toStdString().c_str());
    local_addr.sin_port = htons(lineedit_summary_13.lineedit_2->lineedit->text().toInt());

    unsigned int cmd = 0;
    unsigned int jj = 0;
    unsigned int j = 0;
    unsigned char can_rx_buf[8];
    unsigned int src_cmd = 0;
    unsigned int src_operate_flag = 0;

    memset(can_rx_buf, 0, sizeof(can_rx_buf));
    mutex_udp_tx = CreateMutexA(NULL, false, NULL);

    if (bind(sock, (sockaddr *)&local_addr, sizeof(local_addr)) < 0)
        ret = -2;

    // udp_operate_query();
    // CreateThread(NULL , 0 , udp_heart , NULL , 0 ,(LPDWORD)&ipthread);

    while (1)
    {
        frame_len = recvfrom(sock, (char *)udp_rx_buf, 1536, 0, (sockaddr *)&rx_server, &len);
        if (0 < frame_len)
        {
            int tmp = verify_udp_frame(udp_rx_buf, frame_len);
            if (!tmp)
            {
                cmd_code = *((unsigned short *)&udp_rx_buf[20]);
                ack = udp_rx_buf[22];
                is_ack = udp_rx_buf[23];
                switch (cmd_code)
                {
                case 1101:
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "建立连接反馈";

                        my_textedit_1->my_append(cur_time);
                    }

                    break;
                }

                case 1102:
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "断开连接反馈";

                        my_textedit_1->my_append(cur_time);
                    }

                    break;
                }

                case 7101: // 出光、停光
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        result = *((unsigned int *)&udp_rx_buf[25]);
                        unsigned int result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "出光/停光 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    if ((0 == ack) && (2 == is_ack))
                    {
                        result = *((unsigned int *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "出光/停光 反馈";
                        QString hexString = QString::number(result, 16);
                        cur_time += "0x";
                        cur_time += hexString;
                        my_textedit_1->my_append(cur_time);
                    }
                    break;
                }

                case 7102: // 复位
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "复位 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    break;
                }

                case 7100: // 出光参数设置
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        result = *((unsigned int *)&udp_rx_buf[25]);
                        unsigned char intensity = udp_rx_buf[24];
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "出光功率设置 ";
                        cur_time += to_string(intensity).c_str();
                        cur_time += "% ";
                        cur_time += "ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    break;
                }

                case 7103: // 冷、热待机
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "冷/热待机 ACK(" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    break;
                }

                case 7104: // 撤收
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "撤收 ACK(" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    break;
                }

                case 7105: // 开始参数上传
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "开始参数上传 ACK(" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    break;
                }

                case 7201: // 运行参数反馈
                {
                    memcpy(&operate_parm, &udp_rx_buf[24], sizeof(operate_parm));
                    operate_data_update();
                    udp_alive();
                    if (show_parm_cmd_checkbox->checkbox->isChecked())
                    {
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "运行参数反馈";
                        my_textedit_1->my_append(cur_time);
                    }
                    break;
                }

                case 7110: // 子束适应性参数设置
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned int result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "子束适应性参数设置 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    if ((0 == ack) && (2 == is_ack))
                    {
                        result = *((unsigned int *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "子束适应性参数反馈(0x" + QString::number(result, 16) + ")";
                        my_textedit_1->my_append(cur_time);
                    }
                    break;
                }

                case 7111: // 子束适应性参数存储
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned int result_data = *((unsigned int *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "子束适应性参数存储/初始化 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    if ((0 == ack) && (2 == is_ack))
                    {
                        result = *((unsigned int *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "子束适应性参数存储/初始化反馈(0x" + QString::number(result, 16) + ")";
                        my_textedit_1->my_append(cur_time);
                    }
                    break;
                }

                case 7112: // 集中控制器适应性参数设置
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "集中控制器适应性参数设置 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    if ((0 == ack) && (2 == is_ack))
                    {
                        result = *((unsigned int *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "集中控制器适应性参数设置反馈(0x" + QString::number(result, 16) + ")";
                        my_textedit_1->my_append(cur_time);
                    }
                    break;
                }

                case 7113: // 集中控制器适应性参数存储/初始化
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned int result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "集中控制器适应性参数存储/初始化 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    if ((0 == ack) && (2 == is_ack))
                    {
                        result = *((unsigned int *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "集中控制器适应性参数存储/初始化反馈(0x" + QString::number(result, 16) + ")";
                        my_textedit_1->my_append(cur_time);
                    }
                    break;
                }

                case 7114: // 适应性参数查询反馈
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "适应性参数查询反馈 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    if ((0 == ack) && (2 == is_ack))
                    {
                        if (0 == udp_rx_buf[24])
                        {
                            memcpy(&adaptor_parm.master_conctrller_ada_parm, &udp_rx_buf[25], sizeof(adaptor_parm.master_conctrller_ada_parm));
                        }
                        else if ((1 <= udp_rx_buf[24]) && (32 >= udp_rx_buf[24]))
                        {
                            unsigned char slave_count = udp_rx_buf[24] - 1;
                            memcpy(&adaptor_parm.laser_ada_parm[slave_count], &udp_rx_buf[25], sizeof(adaptor_parm.laser_ada_parm[slave_count]));
                        }

                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "适应性参数查询数据反馈(" + QString::number(udp_rx_buf[24], 10) + ")";
                        my_textedit_1->my_append(cur_time);

                        //if (28 == udp_rx_buf[24])
                            adaptor_data_update();
                    }
                    break;
                }

                case 7115: // 调试模式反馈
                {
                    if ((0 == ack) && (1 == is_ack))
                    {
                        unsigned long long result_data = *((unsigned long long *)&udp_rx_buf[24]);
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;
                        cur_time += "调试模式反馈 ACK(0x" + QString::number(result_data, 16) + ")";

                        my_textedit_1->my_append(cur_time);
                    }

                    if ((0 == ack) && (2 == is_ack))
                    {
                        memcpy(&adaptor_parm, &udp_rx_buf[24], sizeof(adaptor_parm));
                        SYSTEMTIME current_time;
                        GetLocalTime(&current_time);
                        char time_str[100];
                        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                                current_time.wHour,
                                current_time.wMinute,
                                current_time.wSecond,
                                current_time.wMilliseconds);
                        QString cur_time = time_str;

                        cur_time += "调试模式反馈(0x" + QString::number(result, 16) + ")";
                        my_textedit_1->my_append(cur_time);
                    }
                    break;
                }
                }
            }
        }

        if (1 == udp_stop)
        {
            // udp_stop = 0;
            break;
        }
    }

    closesocket(sock);
    return 0;
}

unsigned int get_slave_mount(void)
{
    unsigned int ret = 0;
    for (unsigned int i = 0; i < SLAVE_NUM; i++)
    {
        if (slave_mount_widget.slave_mount[i]->checkbox->isChecked())
            ret |= (1 << i);
    }
    return ret;
}

void set_general_cmd_packet(struct udp_general_packet *cmd_frame, unsigned short cmd_code, unsigned char ack)
{
    memcpy(cmd_frame->protocol_name, "CX100000", sizeof(cmd_frame->protocol_name));
    cmd_frame->len = sizeof(*cmd_frame);
    cmd_frame->len_inverse = ~cmd_frame->len;
    cmd_frame->src = LOACL_ID_FLAG;
    cmd_frame->dst = MASTER_ID_FLAG;
    cmd_frame->cmd_count = udp_cmd_count;
    cmd_frame->cmd_re_count = 0;
    cmd_frame->cmd_code = cmd_code;
    cmd_frame->ack = ack;
    cmd_frame->is_ack = 0;
    cmd_frame->check_sum = 0;
    for (unsigned int i = 0; i < sizeof(struct udp_general_packet) - 4; i++)
    {
        cmd_frame->check_sum += ((unsigned char *)cmd_frame)[i];
    }
    udp_cmd_count++;
}

void send_4bytes_data_cmd(unsigned short cmd_code, unsigned char ack, unsigned char is_ack, unsigned char *data)
{
    struct udp_4bytes_data_cmd_packet cmd_frame;
    int len = sizeof(server);
    memcpy(cmd_frame.protocol_name, "CX100000", sizeof(cmd_frame.protocol_name));
    cmd_frame.len = sizeof(cmd_frame);
    cmd_frame.len_inverse = ~cmd_frame.len;
    cmd_frame.src = LOACL_ID_FLAG;
    cmd_frame.dst = MASTER_ID_FLAG;
    cmd_frame.cmd_count = udp_cmd_count;
    cmd_frame.cmd_re_count = 0;
    cmd_frame.cmd_code = cmd_code;
    cmd_frame.ack = ack;
    cmd_frame.is_ack = is_ack;
    memcpy(cmd_frame.data, data, sizeof(cmd_frame.data));
    cmd_frame.check_sum = 0;
    for (unsigned int i = 0; i < sizeof(cmd_frame) - 4; i++)
    {
        cmd_frame.check_sum += ((unsigned char *)&cmd_frame)[i];
    }
    sendto(sock, (char *)&cmd_frame, cmd_frame.len, 0, (sockaddr *)&server, len);
    udp_cmd_count++;
}

void send_8bytes_data_cmd(unsigned short cmd_code, unsigned char ack, unsigned char is_ack, unsigned char *data)
{
    struct udp_8bytes_data_cmd_packet cmd_frame;
    int len = sizeof(server);
    memcpy(cmd_frame.protocol_name, "CX100000", sizeof(cmd_frame.protocol_name));
    cmd_frame.len = sizeof(cmd_frame);
    cmd_frame.len_inverse = ~cmd_frame.len;
    cmd_frame.src = LOACL_ID_FLAG;
    cmd_frame.dst = MASTER_ID_FLAG;
    cmd_frame.cmd_count = udp_cmd_count;
    cmd_frame.cmd_re_count = 0;
    cmd_frame.cmd_code = cmd_code;
    cmd_frame.ack = ack;
    cmd_frame.is_ack = is_ack;
    memcpy(cmd_frame.data, data, sizeof(cmd_frame.data));
    cmd_frame.check_sum = 0;
    for (unsigned int i = 0; i < sizeof(cmd_frame) - 4; i++)
    {
        cmd_frame.check_sum += ((unsigned char *)&cmd_frame)[i];
    }
    sendto(sock, (char *)&cmd_frame, cmd_frame.len, 0, (sockaddr *)&server, len);
    udp_cmd_count++;
}

void udp_connect(void)
{
    unsigned char data[4];
    memset(data, 0, sizeof(data));
    send_4bytes_data_cmd(7201, 0, 1, data);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "建立连接";
    my_textedit_1->my_append(cur_time);
}

void udp_disconnect(void)
{
    unsigned char data[8];
    memset(data, 0, sizeof(data));
    send_8bytes_data_cmd(1102, 1, 0, data);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "断开连接";
    my_textedit_1->my_append(cur_time);
}

void udp_alive(void)
{
    unsigned char data[8];
    memset(data, 0, sizeof(data));
    send_4bytes_data_cmd(1103, 0, 1, data);

    if (show_parm_cmd_checkbox->checkbox->isChecked())
    {
        SYSTEMTIME current_time;
        GetLocalTime(&current_time);
        char time_str[100];
        sprintf(time_str, "%02d:%02d:%02d.%03d ",
                current_time.wHour,
                current_time.wMinute,
                current_time.wSecond,
                current_time.wMilliseconds);
        QString cur_time = time_str;
        cur_time += "运行参数上报ACK";
        my_textedit_1->my_append(cur_time);
    }
}

void udp_upload_start(void)
{
    unsigned char data[8];
    memset(data, 0, sizeof(data));
    data[0] = 0xAA;
    *((unsigned int *)&data[1]) = get_slave_mount();
    send_8bytes_data_cmd(7105, 1, 0, data);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "开始参数上传";
    my_textedit_1->my_append(cur_time);
}

void udp_fight_parm_set(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    cmd_frame.data[0] = lineedit_summary_5.lineedit_1->lineedit->text().toInt();
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7100, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "作战参数配置";
    my_textedit_1->my_append(cur_time);
}

void udp_output_strong_light(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(&cmd_frame, 0, sizeof(cmd_frame));
    cmd_frame.data[0] = 0xAA;
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7101, 2);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "出强光";
    my_textedit_1->my_append(cur_time);
}

void udp_stop_light(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(&cmd_frame, 0, sizeof(cmd_frame));
    cmd_frame.data[0] = 0x55;
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7101, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "停光";
    my_textedit_1->my_append(cur_time);
}

void udp_output_red_light(void)
{
    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "出红光";
    my_textedit_1->my_append(cur_time);
}

void udp_output_week_1_light(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(&cmd_frame, 0, sizeof(cmd_frame));
    cmd_frame.data[0] = 0x3;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7101, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "出一级弱光";
    my_textedit_1->my_append(cur_time);
}

void udp_output_week_2_light(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(&cmd_frame, 0, sizeof(cmd_frame));
    cmd_frame.data[0] = 0x4;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7101, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "出二级弱光";
    my_textedit_1->my_append(cur_time);
}

void udp_reset(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(&cmd_frame, 0, sizeof(cmd_frame));
    cmd_frame.data[0] = 0xAA;
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7102, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "复位";
    my_textedit_1->my_append(cur_time);
}

void udp_cold_standy_pre(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(cmd_frame.data, 0, sizeof(cmd_frame.data));
    cmd_frame.data[0] = 0x55;
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7103, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "冷待机";
    my_textedit_1->my_append(cur_time);
}

void udp_hot_standy_pre(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(cmd_frame.data, 0, sizeof(cmd_frame.data));
    cmd_frame.data[0] = 0xAA;
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7103, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "热待机";
    my_textedit_1->my_append(cur_time);
}

void udp_withdraw(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    memset(cmd_frame.data, 0, sizeof(cmd_frame.data));
    cmd_frame.data[0] = 0xAA;
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7104, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "撤收";
    my_textedit_1->my_append(cur_time);
}

void udp_state_upload_start(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    cmd_frame.data[0] = 0xAA;
    ;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7105, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "开始状态查询";
    my_textedit_1->my_append(cur_time);
}

void udp_slave_adaptor_set(void)
{
    int len = sizeof(server);
    struct udp_slave_parm_set_packet slave_parm_set_packet;

    fill_slave_set_parm();

    memcpy(slave_parm_set_packet.protocol_name, "CX100000", sizeof(slave_parm_set_packet.protocol_name));
    slave_parm_set_packet.len = sizeof(slave_parm_set_packet);
    slave_parm_set_packet.len_inverse = ~slave_parm_set_packet.len;
    slave_parm_set_packet.src = LOACL_ID_FLAG;
    slave_parm_set_packet.dst = MASTER_ID_FLAG;
    slave_parm_set_packet.cmd_count = udp_cmd_count;
    slave_parm_set_packet.cmd_re_count = 0;
    slave_parm_set_packet.cmd_code = 7110;
    slave_parm_set_packet.ack = 2;
    slave_parm_set_packet.is_ack = 0;
    slave_parm_set_packet.data.slave_mount_flag = get_slave_mount();

    memcpy(&slave_parm_set_packet.data.slave_adaptor_parm, &slave_set_parm, sizeof(slave_parm_set_packet.data.slave_adaptor_parm));

    slave_parm_set_packet.check_sum = 0;
    for (unsigned int i = 0; i < sizeof(slave_parm_set_packet) - 4; i++)
    {
        slave_parm_set_packet.check_sum += ((unsigned char *)&slave_parm_set_packet)[i];
    }
    udp_cmd_count++;

    sendto(sock, (char *)&slave_parm_set_packet, sizeof(slave_parm_set_packet), 0, (sockaddr *)&server, len);
    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "子束适应性参数设置";
    my_textedit_1->my_append(cur_time);
}

void udp_slave_adaptor_store(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;

    cmd_frame.data[0] = 0x0f;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7111, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "子束适应性参数存储";
    my_textedit_1->my_append(cur_time);
}

void udp_slave_adaptor_init(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;

    cmd_frame.data[0] = 0xf0;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7111, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "子束适应性参数初始化";
    my_textedit_1->my_append(cur_time);
}

void udp_master_adaptor_set(void)
{
    int len = sizeof(server);
    struct udp_master_parm_set_packet master_parm_set_packet;

    fill_master_set_parm();

    memcpy(master_parm_set_packet.protocol_name, "CX100000", sizeof(master_parm_set_packet.protocol_name));
    master_parm_set_packet.len = sizeof(master_parm_set_packet);
    master_parm_set_packet.len_inverse = ~master_parm_set_packet.len;
    master_parm_set_packet.src = LOACL_ID_FLAG;
    master_parm_set_packet.dst = MASTER_ID_FLAG;
    master_parm_set_packet.cmd_count = udp_cmd_count;
    master_parm_set_packet.cmd_re_count = 0;
    master_parm_set_packet.cmd_code = 7112;
    master_parm_set_packet.ack = 2;
    master_parm_set_packet.is_ack = 0;
    memcpy(&master_parm_set_packet.data, &master_parm_set, sizeof(master_parm_set_packet.data));

    master_parm_set_packet.check_sum = 0;
    for (unsigned int i = 0; i < sizeof(master_parm_set_packet) - 4; i++)
    {
        master_parm_set_packet.check_sum += ((unsigned char *)&master_parm_set_packet)[i];
    }
    udp_cmd_count++;
    sendto(sock, (char *)&master_parm_set_packet, sizeof(master_parm_set_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "集中控制器适应性参数设置";
    my_textedit_1->my_append(cur_time);
}

void udp_master_adaptor_store(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    cmd_frame.data[0] = 0x0f;
    set_general_cmd_packet(&cmd_frame, 7113, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "集中控制器适应性参数存储";
    my_textedit_1->my_append(cur_time);
}

void udp_master_adaptor_init(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    unsigned char data[8];
    memset(data, 0, sizeof(data));
    data[0] = 0xf0;
    send_4bytes_data_cmd(7113, 2, 0, data);
    // set_general_cmd_packet(&cmd_frame , 7113 , 1);
    // sendto(sock, (char*)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr*)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "集中控制器适应性参数初始化";
    my_textedit_1->my_append(cur_time);
}

#if 0



void udp_adaptor_set(void)
{
    int len = sizeof(server);
    memset(tx_buf , 0 , sizeof(struct eth_ada_parm_set_packet));
    struct eth_ada_parm_set_packet* p_cmd = (struct eth_ada_parm_set_packet*)tx_buf;
    adaptor_parm_data_set();


    p_cmd->start = 0x71746783;
    p_cmd->co_ver = 1;
    p_cmd->src = 0x5A;
    p_cmd->dst = 0x47;
    p_cmd->time_stamp = 0;
    p_cmd->cmd_info = 0x13;              //参数设置
    p_cmd->len = sizeof(struct eth_ada_parm_set_packet);
    memcpy(&p_cmd->ada_parm , &configurable_parm , sizeof(p_cmd->ada_parm));
    p_cmd->check_sum = getCRC((unsigned char*)tx_buf , p_cmd->len - 8);
    p_cmd->end = 0x83717475;
    sendto(sock, tx_buf, sizeof(struct eth_ada_parm_set_packet), 0, (sockaddr*)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str , "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "适应性参数设置";
    my_textedit_1->my_append(cur_time);
}


//适应性参数存储
void udp_adaptor_store(void)
{
    int len = sizeof(server);
    memset(tx_buf , 0 , len);
    struct eth_ada_store_packet* p_cmd = (struct eth_ada_store_packet*)tx_buf;
    p_cmd->start = 0x71746783;
    p_cmd->co_ver = 1;
    p_cmd->src = 0x5A;
    p_cmd->dst = 0x47;
    ftime(&t1);
    p_cmd->time_stamp = (unsigned long long)t1.time * 1000 + t1.millitm;

    p_cmd->cmd_info = 0x15;
    p_cmd->len = sizeof(struct eth_ada_store_packet);
    p_cmd->store_flag =  0x0f;
    p_cmd->check_sum = getCRC((unsigned char*)tx_buf , p_cmd->len - 8);
    p_cmd->end = 0x83717475;
    sendto(sock, tx_buf, sizeof(struct eth_ada_store_packet), 0, (sockaddr*)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str , "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "适应性参数存储";
    my_textedit_1->my_append(cur_time);
}

void udp_adaptor_init(void)
{
    int len = sizeof(server);
    memset(tx_buf , 0 , len);
    struct eth_ada_store_packet* p_cmd = (struct eth_ada_store_packet*)tx_buf;
    p_cmd->start = 0x71746783;
    p_cmd->co_ver = 1;
    p_cmd->src = 0x5A;
    p_cmd->dst = 0x47;
    p_cmd->time_stamp = 0;
    p_cmd->cmd_info = 0x15;              //出光

    p_cmd->len = sizeof(struct eth_ada_store_packet);
    p_cmd->store_flag =  0xf0;
    p_cmd->check_sum = getCRC((unsigned char*)tx_buf , p_cmd->len - 8);
    p_cmd->end = 0x83717475;
    sendto(sock, tx_buf, sizeof(struct eth_ada_store_packet), 0, (sockaddr*)&server, len);

}

#endif

void udp_adaptor_query(void)
{
    int len = sizeof(server);
    struct udp_general_packet cmd_frame;
    cmd_frame.data[0] = 0xAA;
    *((unsigned int *)&cmd_frame.data[1]) = get_slave_mount();
    set_general_cmd_packet(&cmd_frame, 7114, 1);
    sendto(sock, (char *)&cmd_frame, sizeof(struct udp_general_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "适应性参数查询";
    my_textedit_1->my_append(cur_time);
}

void udp_operate_query(void)
{
    int len = sizeof(server);
    memset(tx_buf, 0, len);
    struct eth_operate_parm_query_packet *p_cmd = (struct eth_operate_parm_query_packet *)tx_buf;
    p_cmd->start = 0x71746783;
    p_cmd->co_ver = 1;
    p_cmd->src = 0x5A;
    p_cmd->dst = 0x47;
    p_cmd->time_stamp = 0;
    p_cmd->cmd_info = 0x19; // 出光
    p_cmd->len = sizeof(struct eth_operate_parm_query_packet);
    p_cmd->check_sum = getCRC((unsigned char *)tx_buf, p_cmd->len - 8);
    p_cmd->end = 0x83717475;
    sendto(sock, tx_buf, sizeof(struct eth_operate_parm_query_packet), 0, (sockaddr *)&server, len);
}

void udp_enter_debug(void)
{
    int len = sizeof(server);
    struct udp_4bytes_data_cmd_packet cmd_frame;
    unsigned char data[8];
    memset(data, 0, sizeof(data));
    data[0] = 0x0f;
    send_8bytes_data_cmd(7115, 2, 0, data);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "进入调试模式";
    my_textedit_1->my_append(cur_time);
}

void udp_exit_debug(void)
{
    int len = sizeof(server);
    struct udp_4bytes_data_cmd_packet cmd_frame;
    unsigned char data[8];
    memset(data, 0, sizeof(data));
    data[0] = 0xf0;
    send_4bytes_data_cmd(7115, 2, 0, data);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "退出调试模式";
    my_textedit_1->my_append(cur_time);
}

DWORD WINAPI udp_heart(LPVOID pParam)
{
    int len = sizeof(server);
    // memset(tx_buf , 0 , len);
    struct eth_heart_packet p_cmd;
    p_cmd.start = 0x71746783;
    p_cmd.co_ver = 1;
    p_cmd.src = 0x5A;
    p_cmd.dst = 0x47;
    p_cmd.time_stamp = 0;
    p_cmd.cmd_info = 0; // 心跳
    p_cmd.len = sizeof(struct eth_heart_packet);
    p_cmd.check_sum = getCRC((unsigned char *)&p_cmd, p_cmd.len - 8);
    p_cmd.end = 0x83717475;
    while (1)
    {
        sendto(sock, (char *)&p_cmd, sizeof(struct eth_heart_packet), 0, (sockaddr *)&server, len);
        Sleep(500);
        if (1 == udp_stop)
        {
            // udp_stop = 0;
            break;
        }
    }
}

void udp_enter_week_light_mode(void)
{
    int len = sizeof(server);
    memset(tx_buf, 0, len);
    struct eth_week_light_mode_packet *p_cmd = (struct eth_week_light_mode_packet *)tx_buf;
    p_cmd->start = 0x71746783;
    p_cmd->co_ver = 1;
    p_cmd->src = 0x5A;
    p_cmd->dst = 0x47;
    p_cmd->time_stamp = 0;
    p_cmd->cmd_info = 0x1b; // 进入弱光模式
    p_cmd->operate_flag = 0x0f;
    p_cmd->enable_config = 0x3;
    p_cmd->len = sizeof(struct eth_week_light_mode_packet);

    p_cmd->check_sum = getCRC((unsigned char *)tx_buf, p_cmd->len - 8);
    p_cmd->end = 0x83717475;
    sendto(sock, tx_buf, sizeof(struct eth_week_light_mode_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "进入弱光模式";
    my_textedit_1->my_append(cur_time);
}

void udp_exit_week_light_mode(void)
{
    int len = sizeof(server);
    memset(tx_buf, 0, len);
    struct eth_week_light_mode_packet *p_cmd = (struct eth_week_light_mode_packet *)tx_buf;
    p_cmd->start = 0x71746783;
    p_cmd->co_ver = 1;
    p_cmd->src = 0x5A;
    p_cmd->dst = 0x47;
    p_cmd->time_stamp = 0;
    p_cmd->cmd_info = 0x1b;
    p_cmd->operate_flag = 0xf0; // 退出弱光模式
    p_cmd->enable_config = 0x3;
    p_cmd->len = sizeof(struct eth_week_light_mode_packet);

    p_cmd->check_sum = getCRC((unsigned char *)tx_buf, p_cmd->len - 8);
    p_cmd->end = 0x83717475;
    sendto(sock, tx_buf, sizeof(struct eth_week_light_mode_packet), 0, (sockaddr *)&server, len);

    SYSTEMTIME current_time;
    GetLocalTime(&current_time);
    char time_str[100];
    sprintf(time_str, "%02d:%02d:%02d.%03d ",
            current_time.wHour,
            current_time.wMinute,
            current_time.wSecond,
            current_time.wMilliseconds);
    QString cur_time = time_str;
    cur_time += "退出弱光模式";
    my_textedit_1->my_append(cur_time);
}
